1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import static com.google.common.truth.Truth.assertThat;
20  import static java.util.Arrays.asList;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.collect.testing.google.TestStringMultisetGenerator;
24  
25  import junit.framework.TestCase;
26  
27  import java.util.Arrays;
28  import java.util.List;
29  
30  /**
31   * Unit test for {@link LinkedHashMultiset}.
32   *
33   * @author Kevin Bourrillion
34   */
35  @GwtCompatible(emulated = true)
36  public class LinkedHashMultisetTest extends TestCase {
37  
38    private static TestStringMultisetGenerator linkedHashMultisetGenerator() {
39      return new TestStringMultisetGenerator() {
40        @Override protected Multiset<String> create(String[] elements) {
41          return LinkedHashMultiset.create(asList(elements));
42        }
43  
44        @Override
45        public List<String> order(List<String> insertionOrder) {
46          List<String> order = Lists.newArrayList();
47          for (String s : insertionOrder) {
48            int index = order.indexOf(s);
49            if (index == -1) {
50              order.add(s);
51            } else {
52              order.add(index, s);
53            }
54          }
55          return order;
56        }
57      };
58    }
59  
60    public void testCreate() {
61      Multiset<String> multiset = LinkedHashMultiset.create();
62      multiset.add("foo", 2);
63      multiset.add("bar");
64      assertEquals(3, multiset.size());
65      assertEquals(2, multiset.count("foo"));
66      assertEquals("[foo x 2, bar]", multiset.toString());
67    }
68  
69    public void testCreateWithSize() {
70      Multiset<String> multiset = LinkedHashMultiset.create(50);
71      multiset.add("foo", 2);
72      multiset.add("bar");
73      assertEquals(3, multiset.size());
74      assertEquals(2, multiset.count("foo"));
75      assertEquals("[foo x 2, bar]", multiset.toString());
76    }
77  
78    public void testCreateFromIterable() {
79      Multiset<String> multiset
80          = LinkedHashMultiset.create(Arrays.asList("foo", "bar", "foo"));
81      assertEquals(3, multiset.size());
82      assertEquals(2, multiset.count("foo"));
83      assertEquals("[foo x 2, bar]", multiset.toString());
84    }
85  
86    public void testToString() {
87      Multiset<String> ms = LinkedHashMultiset.create();
88      ms.add("a", 3);
89      ms.add("c", 1);
90      ms.add("b", 2);
91  
92      assertEquals("[a x 3, c, b x 2]", ms.toString());
93    }
94  
95    public void testLosesPlaceInLine() throws Exception {
96      Multiset<String> ms = LinkedHashMultiset.create();
97      ms.add("a");
98      ms.add("b", 2);
99      ms.add("c");
100     assertThat(ms.elementSet()).has().exactly("a", "b", "c").inOrder();
101     ms.remove("b");
102     assertThat(ms.elementSet()).has().exactly("a", "b", "c").inOrder();
103     ms.add("b");
104     assertThat(ms.elementSet()).has().exactly("a", "b", "c").inOrder();
105     ms.remove("b", 2);
106     ms.add("b");
107     assertThat(ms.elementSet()).has().exactly("a", "c", "b").inOrder();
108   }
109 }
110